package com.mall.untils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description: <br>
 * 2010-5-12
 *
 * @author huxiao kskr@qq.com
 */
public class CookieUtil {

	private static Logger logger = LoggerFactory.getLogger(CookieUtil.class);
	
    private final static int maxAge = 60 * 60 * 60 * 24 * 365;
    private final static String uri = "/";

    private CookieUtil() {}

    public static void setAttribute(String key, String value, HttpServletResponse response) {
    	logger.debug("=====set cookie name======>" + key);
    	logger.debug("=====set cookie value======>" + value);
    	
        Cookie cookie = new Cookie(key, value);
        cookie.setMaxAge(maxAge);
        cookie.setPath(uri);
        cookie.setDomain(".vvpinche.com");
        response.addCookie(cookie);
    }

    public static String getAttribute(String key, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(key)) {
                	logger.debug("=====cookie name======>" + cookie.getName());
                	logger.debug("=====cookie name======>" + cookie.getValue());
                	
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static void removeAllAttribute(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                removeAttribute(cookie.getName(), response);
            }
        }
    }

    public static void removeAttribute(String key, HttpServletResponse response) {
        Cookie cookie = new Cookie(key, null);
        cookie.setMaxAge(0);
        cookie.setPath(uri);
        response.addCookie(cookie);
    }
    
    ////////////////////////2015.08.7
	
	/**
	 * 设置cookie，默认最大过期时间
	 * @param response
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletResponse response, String name,
			String value) {
		setCookie(response, name, value, maxAge);
	}
	/**
	 * 设置cookie，自定义最大过期时间
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAge
	 */
	public static void setCookie(HttpServletResponse response, String name,
			String value, int maxAge) {
		Cookie cookie = new Cookie(name, null);
		cookie.setPath("/");
		cookie.setMaxAge(maxAge);
		cookie.setValue(value);
		response.addCookie(cookie);
	}
	

	/**
	 * 获取cookie，不删除
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		return getCookie(request, null, name, false);
	}

	/**
	 * 获取cookie，删除
	 * @param request
	 * @param response
	 * @param name
	 * @return
	 */
	public static String getCookie(HttpServletRequest request,
			HttpServletResponse response, String name) {
		return getCookie(request, response, name, true);
	}
	/**
	 * 获取cookie，自定义是否删除
	 * @param request
	 * @param response
	 * @param name
	 * @param isRemove
	 * @return
	 */
	public static String getCookie(HttpServletRequest request,
			HttpServletResponse response, String name, boolean isRemove) {
		String value = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name)) {
					value = cookie.getValue();
					if (isRemove) {
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			}
		}
		return value;
	}
}

